package com.example.server;

import java.io.InputStream;
import java.util.Timer;
import java.util.TimerTask;

import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.view.animation.Transformation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

import com.example.silvercare.R;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;

public class MapViewActivity extends MapActivity{

	ImageView imgView;
	ImageView imgView2;
	
	float PreX;
	float PreY;
	//simulation
	Timer timer = new Timer();
	TranslateAnimation translate;
	final Handler UpdateHandler = new Handler(){
		public void handleMessage(Message msg) {
			switch (msg.what) {      
		             case 1:
		            	 imgView2.startAnimation(translate); 
		                  break;     
		              }      
		              super.handleMessage(msg);  
		         }    
		     }; 
		     
     //Get Single Friend Location
	String latitude;
	String longitude;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mapview);
        
        //MapView mapView = (MapView) findViewById(R.id.map1);
        
        imgView = (ImageView)findViewById(R.id.ImageView02);
        imgView.setImageResource(R.drawable.map);
        
        imgView2 = (ImageView)findViewById(R.id.ImageView03);
        imgView2.setImageResource(R.drawable.person);
        
        PreX = 0;
        PreY = 0;
        
        latitude = getIntent().getStringExtra("LATITUDE");
        longitude = getIntent().getStringExtra("LONGITUDE");

        if(latitude != null && longitude != null){
        	if(latitude.equalsIgnoreCase("Waiting") || longitude.equalsIgnoreCase("Waiting"))
        	{
        		//do nothing
        	}else
        	{
        		float newX = (15+Float.parseFloat(latitude)*90)*2/3-24;
				float newY = (138+Float.parseFloat(longitude)*90)*2/3-24;
        		translate = new TranslateAnimation(PreX, newX, PreY, newY);
				//translate = new TranslateAnimation(0,10-24,0,92-24);
				PreX = newX;
		        PreY = newY;
		        translate.setFillAfter(true);
		        translate.setDuration(500);
		        imgView2.startAnimation(translate); 
        	}
        }
        else
        	simulateMyCurrentLocation();
    }
    
	private void simulateMyCurrentLocation(){
	
		timer.scheduleAtFixedRate(new TimerTask() {

			public void run() {
				String latitude = LocationUpdateService.latitude;
				String longitude = LocationUpdateService.longitude;
				
				if(latitude!=null && longitude!=null)
				{
					float newX = (15+Float.parseFloat(latitude)*90)*2/3-24;
					float newY = (138+Float.parseFloat(longitude)*90)*2/3-24;
			        translate = new TranslateAnimation(PreX, newX, PreY, newY);
					//translate = new TranslateAnimation(0,10-24,0,92-24);
					PreX = newX;
			        PreY = newY;
			        translate.setFillAfter(true);
			        translate.setDuration(500);

					Message message = new Message();      
					message.what = 1;      
					UpdateHandler.sendMessage(message);  
				}
			}
		}, 0000, 5000);
	}
	
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		timer.cancel();
		super.onDestroy();
	}



}
